package com.chiffoyqc.task;

import com.alibaba.fastjson.JSON;
import com.chiffoyqc.config.URLConfig;
import com.chiffoyqc.dao.DBWireLess.CF_UserInfDao_wireLess;
import com.chiffoyqc.model.AlarmInfo;
import com.chiffoyqc.model.DBWireLess.CF_UserInf_wireLess;
import com.chiffoyqc.utils.ApplicationContextUtil;
import com.chiffoyqc.utils.HttpHelper;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.FutureTask;

/**
 * Created by Administrator on 2018/7/23.
 */
public class ConnectionExeception_task implements Runnable {
    @Override
    public void run() {
        System.out.println("开始通讯异常检测");
        /**查询未删除的用户编号*/
        CF_UserInfDao_wireLess userInfDao = ApplicationContextUtil.getBean(CF_UserInfDao_wireLess.class);
        try {
            List<CF_UserInf_wireLess> userInfs = userInfDao.queryAll();
            int length = userInfs.size();
            int num = length / 4;
            int last = length % 4;

            List<CF_UserInf_wireLess> list1 = userInfs.subList(0, num);
            List<CF_UserInf_wireLess> list2 = userInfs.subList(num, num * 2);
            List<CF_UserInf_wireLess> list3 = userInfs.subList(num * 2 , num * 3);
            List<CF_UserInf_wireLess> list4 = userInfs.subList(num * 3 , num * 4 + last);

            ConnectionExeception_TaskItem taskItem1 = new ConnectionExeception_TaskItem(list1);
            FutureTask<List<AlarmInfo>> futureTask1 = new FutureTask<>(taskItem1);
            futureTask1.run();

            ConnectionExeception_TaskItem taskItem2 = new ConnectionExeception_TaskItem(list2);
            FutureTask<List<AlarmInfo>> futureTask2 = new FutureTask<>(taskItem2);
            futureTask2.run();

            ConnectionExeception_TaskItem taskItem3 = new ConnectionExeception_TaskItem(list3);
            FutureTask<List<AlarmInfo>> futureTask3 = new FutureTask<>(taskItem3);
            futureTask3.run();

            ConnectionExeception_TaskItem taskItem4 = new ConnectionExeception_TaskItem(list4);
            FutureTask<List<AlarmInfo>> futureTask4 = new FutureTask<>(taskItem4);
            futureTask4.run();

            List<AlarmInfo> alarmInfos1 = futureTask1.get();
            List<AlarmInfo> alarmInfos2 = futureTask2.get();
            List<AlarmInfo> alarmInfos3 = futureTask3.get();
            List<AlarmInfo> alarmInfos4 = futureTask4.get();

            List<AlarmInfo> totalList = new LinkedList<>();
            totalList.addAll(alarmInfos1);
            totalList.addAll(alarmInfos2);
            totalList.addAll(alarmInfos3);
            totalList.addAll(alarmInfos4);

            if(totalList.size() != 0){
                Map<String,Object> dataMap = new HashMap<>();
                dataMap.put("alarms",totalList);
                String postParm = JSON.toJSONString(dataMap);
                System.out.println("通讯异常上报数据："+postParm);
                String result = HttpHelper.sendPostForRMIS(URLConfig.RMIS_ADDRESS, postParm, "utf-8");
                System.out.println("RMIS返回数据："+result);
            }



//            System.out.println("通讯异常上报处理完成，总数："+ (count1 + count2 + count3 + count4));


        } catch (Exception e) {
            e.printStackTrace();
        }
        /**查询通讯记录*/

    }
}
